﻿#region 程序集引用
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
#endregion

#region 版权注释
/*===========================================================================
// Copyright (C) 2017 Zhang.cn，并保留所有 
// 作者：吴岸标 QQ:405403610
// 创建日期：2017-09-03
// 功能描述： 图片操作
===========================================================================*/
#endregion

namespace Zhang
{
    namespace Utils
    {
        public class OpImage
        {
            //颜色列表，用于验证码、噪线、噪点 
            private static Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
            /// <summary>
            /// 生成验证码图片
            /// </summary>
            /// <param name="imgStr">图片显示内容（字符串）</param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="bgColor">图片背景色</param>
            /// <param name="fontColor">字体颜色</param>
            /// <returns>生成图片的内存流</returns>
            public static MemoryStream VCodeImage(string imgStr, int fontSize, Color bgColor, Color fontColor)
            {
                Bitmap bitMap = null;
                Graphics graphics = null;
                MemoryStream imageStream = null;
                Random rnd;
                int width = 0;
                int height = 0;

                rnd = new Random();
                width = imgStr.Length * fontSize + 8;
                height = fontSize * 2;
                bitMap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                graphics = Graphics.FromImage(bitMap);
                imageStream = new MemoryStream();
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                graphics.Clear(bgColor);

                //画噪线 
                for (int i = 0; i < 20; i++)
                {
                    int x1 = rnd.Next(100);
                    int y1 = rnd.Next(40);
                    int x2 = rnd.Next(100);
                    int y2 = rnd.Next(40);
                    Color clr = color[rnd.Next(color.Length)];
                    graphics.DrawLine(new Pen(clr), x1, y1, x2, y2);
                }

                graphics.DrawString(imgStr, new Font(FontFamily.GenericSerif, 14), new SolidBrush(fontColor), 0, 0);
                bitMap.Save(imageStream, ImageFormat.Gif);
                return imageStream;
            }

            /// <summary>
            /// 生成验证码图片
            /// </summary>
            /// <param name="imgStr">图片显示内容（字符串）</param>
            /// <param name="fontSize">字体大小</param>
            /// <param name="bgColor">图片背景色</param>
            /// <param name="fontColor">字体颜色</param>
            /// <returns>生成图片的内存流</returns>
            public static MemoryStream VCodeImageNoBg(string imgStr, int fontSize, Color bgColor, Color fontColor)
            {
                Bitmap bitMap = null;
                Graphics graphics = null;
                MemoryStream imageStream = null;
                int width = 0;
                int height = 0;

                width = imgStr.Length * fontSize + 8;
                height = fontSize * 2;
                bitMap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                graphics = Graphics.FromImage(bitMap);
                imageStream = new MemoryStream();
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                graphics.Clear(bgColor);
                graphics.DrawString(imgStr, new Font(FontFamily.GenericSerif, 14), new SolidBrush(fontColor), 0, 0);
                bitMap.Save(imageStream, ImageFormat.Gif);
                return imageStream;
            }

            /// <summary>
            /// 產生圖形驗證碼。
            /// </summary>
            /// <param name="Code">傳出驗證碼。</param>
            /// <param name="CodeLength">驗證碼字元數。</param>
            /// <param name="Width"></param>
            /// <param name="Height"></param>
            /// <param name="FontSize"></param>
            /// <returns></returns>
            public static byte[] VCodeImage(String Code, int FontSize)
            {
                int Width = Code.Length * FontSize + 8;
                int Height = FontSize * 2;
                //顏色列表，用於驗證碼、噪線、噪點
                Color[] oColors ={
             System.Drawing.Color.Black,
             System.Drawing.Color.Red,
             System.Drawing.Color.Blue,
             System.Drawing.Color.Green,
             System.Drawing.Color.Orange,
             System.Drawing.Color.Brown,
             System.Drawing.Color.Brown,
             System.Drawing.Color.DarkBlue
            };
                //字體列表，用於驗證碼
                string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
                Random oRnd = new Random();
                Bitmap oBmp = null;
                Graphics oGraphics = null;
                int N1 = 0;
                System.Drawing.Point oPoint1 = default(System.Drawing.Point);
                System.Drawing.Point oPoint2 = default(System.Drawing.Point);
                string sFontName = null;
                Font oFont = null;
                Color oColor = default(Color);

                oBmp = new Bitmap(Width, Height);
                oGraphics = Graphics.FromImage(oBmp);
                oGraphics.Clear(System.Drawing.Color.White);
                try
                {
                    for (N1 = 0; N1 <= 4; N1++)
                    {
                        //畫噪線
                        oPoint1.X = oRnd.Next(Width);
                        oPoint1.Y = oRnd.Next(Height);
                        oPoint2.X = oRnd.Next(Width);
                        oPoint2.Y = oRnd.Next(Height);
                        oColor = oColors[oRnd.Next(oColors.Length)];
                        oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
                    }

                    float spaceWith = 0, dotX = 0, dotY = 0;
                    if (Code.Length != 0)
                    {
                        spaceWith = (Width - FontSize * Code.Length - 10) / Code.Length;
                    }

                    for (N1 = 0; N1 <= Code.Length - 1; N1++)
                    {
                        //畫驗證碼字串
                        sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
                        oFont = new Font(sFontName, FontSize, FontStyle.Italic);
                        oColor = oColors[oRnd.Next(oColors.Length)];

                        dotY = (Height - oFont.Height) / 2 + 2;//中心下移2像素
                        dotX = Convert.ToSingle(N1) * FontSize + (N1 + 1) * spaceWith;

                        oGraphics.DrawString(Code[N1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
                    }

                    for (int i = 0; i <= 30; i++)
                    {
                        //畫噪點
                        int x = oRnd.Next(oBmp.Width);
                        int y = oRnd.Next(oBmp.Height);
                        Color clr = oColors[oRnd.Next(oColors.Length)];
                        oBmp.SetPixel(x, y, clr);
                    }
                    //保存图片数据
                    MemoryStream stream = new MemoryStream();
                    oBmp.Save(stream, ImageFormat.Jpeg);
                    //输出图片流
                    return stream.ToArray();
                }
                finally
                {
                    oGraphics.Dispose();
                }
            }
        }
    }
}